var CreateOrder = function () {
  var template = /* html */ `
    <div class="create-order return-order" v-loading.fullscreen.lock="loading">
      <search-btn-view immediate btn-text="物料查询" @before-open="resetSearch" @search="searchData">
        <template slot="append">
          <el-cascader filterable style="width:300px;margin-left:20px" :options="deptWorkshopList" v-model="deptWork" :props="{value:'dataId',label:'name'}"
            separator="-" placeholder="请选择返料给车间的人员" size="medium"></el-cascader>
          <el-button type="primary" size="medium" @click="returnMaterial('workshop')">返料到车间</el-button>
          <el-button type="primary" size="medium" @click="returnMaterial('outOrder')">返料到外协</el-button>
        </template>
        <div slot="search-content">
          <sys-common-search request-url="materialSelectFilter/getMaterialSelectData" :request-data="{type:20}" :search="search"
            :search-field="searchField" label-width="100px" input-width="180px">
          </sys-common-search>
        </div>
      </search-btn-view>
      <div class="order-table-div">
        <el-table fit stripe border :data="materialList" height="parent" @selection-change="handleSelectionChange">
          <el-table-column sortable prop="companyOrderId" label="订单编号" min-width="130px" header-align="center"></el-table-column>
          <el-table-column sortable prop="factoryNo" label="工厂型号" min-width="125px" header-align="center"></el-table-column>
          <el-table-column sortable prop="customerNo" label="客户型号" min-width="125px" header-align="center"></el-table-column>
          <el-table-column sortable prop="providerName" label="供应商" min-width="120px" header-align="center"></el-table-column>
          <el-table-column sortable prop="materialName" label="物料名称" min-width="120px" header-align="center"></el-table-column>
          <el-table-column sortable prop="model" label="规格" min-width="120px" header-align="center"></el-table-column>
          <el-table-column sortable prop="color" label="编号" min-width="120px" header-align="center"></el-table-column>
          <el-table-column sortable prop="colorDescription" label="描述" min-width="120px" header-align="center"></el-table-column>
          <el-table-column sortable prop="materialDeptName" label="物料部门" min-width="120px" align="center"></el-table-column>
          <el-table-column sortable show-overflow-tooltip label="可出库数" min-width="120px" header-align="center">
            <template slot-scope="scope">
              <div class="table-count">
                <div style="flex:2">{{scope.row.canOutCount}}</div>
                <div style="flex:1;margin-left:3px">{{scope.row.inOutUnit}}</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column sortable prop="materialCount" label="库存数" min-width="120px" header-align="center">
            <template slot-scope="scope">
              <div class="table-count" v-if="scope.row.materialCount!==null">
                <div style="flex:2">{{scope.row.materialCount}}</div>
                <div style="flex:1;margin-left:3px">{{scope.row.inOutUnit}}</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" label="返料数" width="130px" header-align="center">
            <template slot-scope="scope">
              <el-input-number v-if="scope.row.maxCount!==null" is-decimal-zero v-model="scope.row.receiveCount" :controls="false" :precision="scope.row.countPrecision" 
                style="width:120px" :min="1/Math.pow(10,scope.row.countPrecision)" :max="scope.row.maxCount" size="mini"></el-input-number>
            </template>
          </el-table-column>
          <el-table-column sortable prop="feedTotal" label="订单投料数" min-width="120px" align="center"></el-table-column>
          <el-table-column :resizable="false" label="交接数(付)" width="130px" header-align="center">
            <template slot-scope="scope">
              <el-input-number v-if="scope.row.selfOutFuCount!==null" v-model="scope.row.selfOutFuCount" :controls="false" :precision="0"
                style="width:120px" :min="1" size="mini"></el-input-number>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" label="备注" width="150px" header-align="center">
            <template slot-scope="scope">
              <el-input v-model="scope.row.remark" style="width:140px;" size="mini"></el-input>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" type="selection" :selectable="checkCanSelect" fixed="right" width="40px" align="center"></el-table-column>
        </el-table>
      </div>
      <table-pagination v-if="pagination.totalCount!==0" :pagination="pagination" @change="pageChange"></table-pagination>
    </div>
  `;

  return {
    mixins: [
      tablePaginationMixin()
    ],

    data: function () {
      var search = {
        materialName: '',
        model: '',
        color: '',
        providerName: '',
        materialDept: '',
        companyOrderId: '',
        orderId: '',
        factoryNo: '',
        customerNo: ''
      };
      return {
        loading: false,
        search: _.cloneDeep(search),
        oldSearch: _.cloneDeep(search),
        searchField: ['materialName', 'model', 'color', 'providerName', 'materialDept', 'companyOrderId', 'orderId', 'factoryNo', 'customerNo'],
        materialList: [],
        selectMaterialList: [],
        deptWorkshopList: [],
        deptWork: []
      };
    },

    created: function () {
      this.setDeptWorkshopList();
    },

    methods: {
      setDeptWorkshopList: function () {
        var self = this;
        DropdownUtil.getDeptWorkshopUserList().then(function (deptWorkshopList) {
          self.deptWorkshopList = deptWorkshopList;
        }).catch(function () {
          self.deptWorkshopList = [];
        });
      },

      setMaterialList: function (isSearch) {
        if (!isSearch) {
          this.search = _.cloneDeep(this.oldSearch);
        }

        var pagination = this.pagination;
        var currentPage = pagination.currentPage;
        var pageSize = pagination.pageSize;
        var start = (currentPage - 1) * pageSize;
        var postData = {
          start: start,
          length: pageSize,
        }

        var search = this.search;
        var fields = ['materialName', 'model', 'color', 'providerName', 'companyOrderId', 'orderId', 'factoryNo', 'customerNo'];
        SysUtil.setPostData(postData, search, fields);

        var materialDept = search.materialDept;
        if (materialDept !== '') {
          postData.materialDeptId = materialDept.materialDeptId;
        }

        this.loading = true;
        var self = this;
        ajaxRequest("returnWarehouseToProvider/getCanWorkShopToWarehouseData", postData, false).then(function (res) {
          if (isSearch) {
            self.oldSearch = _.cloneDeep(self.search);
          }

          var materialList = res.returnData;
          for (var i = 0; i < materialList.length; i++) {
            var material = materialList[i];
            material.remark = '';
            var receiveCount = Math.min(material.canOutCount, material.materialCount);
            if (receiveCount === 0) {
              receiveCount = null;
            }
            material.receiveCount = receiveCount;
            material.maxCount = receiveCount;

            var inOutUnit = material.inOutUnit;
            var countPrecision = SysUtil.getMaterialNumberPrecision(inOutUnit, 4);
            material.countPrecision = countPrecision;

            if (material.orderId !== null && inOutUnit !== '付') {
              material.selfOutFuCount = receiveCount;
            } else {
              material.selfOutFuCount = null;
            }
            SysUtil.tableSetDeptData(material);
          }

          self.materialList = materialList;
          self.pagination.totalCount = res.iTotalRecords;
          self.loading = false;
        }).catch(function () {
          self.loading = false;
        });
      },

      resetSearch: function () {
        this.search = _.cloneDeep(this.oldSearch);
      },

      searchData: function () {
        this.pagination.currentPage = 1;
        this.setMaterialList(true);
      },

      pageChange: function () {
        this.setMaterialList();
      },

      handleSelectionChange: function (selectMaterialList) {
        this.selectMaterialList = selectMaterialList;
      },

      checkCanSelect: function (material) {
        return material.maxCount !== null;
      },

      returnMaterial: function (type) {
        var selectMaterialList = this.selectMaterialList;
        if (selectMaterialList.length === 0) {
          this.$alert('请选择需要返还的物料', '信息');
          return;
        }

        var postData = {
          warehouseType: 4,
          sourceType: 3,
          type: 2,
        };
        if (type === 'workshop') {
          var deptWork = this.deptWork;
          if (deptWork.length === 0) {
            this.$alert('请选择返料给车间的人员', '信息');
            return;
          }

          deptWork = ModuleUtil.getDeptWorkshop(deptWork);
          postData.deptId = deptWork.deptId;
          postData.workShopId = deptWork.workShopId;
          postData.pickingPeople = deptWork.pickingPeople;
        } else {
          postData.source = 2;
        }

        var postDataList = [];
        var fields = ['materialCode', 'model', 'color', 'providerName', 'orderId', 'rowIndex', 'materialType',
          'remark', 'inOutUnit'
        ];
        for (var i = 0; i < selectMaterialList.length; i++) {
          var material = selectMaterialList[i];
          var receiveCount = material.receiveCount;
          if (receiveCount === undefined) {
            this.$alert('请输入返料数', '信息');
            return;
          }

          var selfOutFuCount = material.selfOutFuCount;
          if (selfOutFuCount !== null) {
            if (selfOutFuCount === undefined) {
              this.$alert('请输入交接数', '信息');
              return;
            }
          }

          var orderPostData = _.cloneDeep(postData);
          orderPostData.count = material.receiveCount;
          if (selfOutFuCount !== null) {
            orderPostData.selfOutFuCount = selfOutFuCount;
          }
          SysUtil.setPostData(orderPostData, material, fields, false, true);
          postDataList.push(orderPostData);
        }

        this.loading = true;
        var self = this;
        ajaxRequest('returnWarehouseToProvider/saveWorkShopToWarehouseData', {
          returnToWorkShopData: JSON.stringify(postDataList)
        }).then(function (msg) {
          self.$alert(msg, '信息', {
            callback: function () {
              self.setMaterialList();
            }
          });
          self.loading = false;
        }).catch(function () {
          self.loading = false;
        });
      }
    },

    components: {
      SysCommonSearch: SysCommonSearch,
      SearchBtnView: SearchBtnView
    },

    template: template
  };
}();
